﻿
using LPP.Entity.Booking;
using LPP.Repository.BookingRepository;


namespace LPP.Service.BookingService
{
    public class BookingService : IBookingService
    {
        private readonly IBookingRepository _bookingRepository;

        public BookingService(IBookingRepository bookingRepository)
        {
            _bookingRepository = bookingRepository ?? throw new ArgumentNullException(nameof(bookingRepository));
        }

        public async Task<bool> CreateBookingAsync(Booking booking)
        {
            if (booking == null)
            {
                throw new ArgumentNullException(nameof(booking), "Booking data cannot be null.");
            }

            booking.Status = BookingStatus.Pending;

            return await _bookingRepository.AddBookingAsync(booking);
        }

        public async Task<Booking> GetBookingByIdAsync(int id)
        {
            var booking = await _bookingRepository.GetBookingByIdAsync(id);
            if (booking == null) return null;

            return new Booking
            {
                Id = booking.Id,
                UserId = booking.UserId,
                ServiceId = booking.ServiceId,
                BookingTime = booking.BookingTime,
                Status = booking.Status // ✅ 直接赋值，不要 ToString()
            };
        }

        public async Task<List<Booking>> GetAllBookingsAsync()
        {
            var bookings = await _bookingRepository.GetAllBookingsAsync();
            return bookings?.Select(booking => new Booking
            {
                Id = booking.Id,
                UserId = booking.UserId,
                ServiceId = booking.ServiceId,
                BookingTime = booking.BookingTime,
                Status = booking.Status // ✅ 直接赋值，不要 ToString()
            }).ToList();
        }

        public async Task<bool> UpdateBookingAsync(int id, Booking bookingDto)
        {
            if (id <= 0 || bookingDto == null)
            {
                return false;
            }

            var booking = await _bookingRepository.GetBookingByIdAsync(id);
            if (booking == null)
            {
                return false;
            }

            booking.UserId = bookingDto.UserId;
            booking.ServiceId = bookingDto.ServiceId;
            booking.BookingTime = bookingDto.BookingTime;
            booking.Status = bookingDto.Status; // ✅ 直接赋值，不要 TryParse()

            return await _bookingRepository.UpdateBookingAsync(booking);
        }

        public async Task<bool> CancelBookingAsync(int id)
        {
            var booking = await _bookingRepository.GetBookingByIdAsync(id);
            if (booking == null)
            {
                throw new KeyNotFoundException("Booking not found.");
            }

            booking.Status = BookingStatus.Cancelled;
            var updated = await _bookingRepository.UpdateBookingAsync(booking);
            if (!updated)
            {
                throw new Exception("Failed to cancel booking.");
            }

            return true;
        }
    }
}
